package com.ruoyi.order.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.Product;
import com.ruoyi.order.domain.app.Vo.CartItem;
import com.ruoyi.order.domain.app.Vo.ShopMoneyVo;
import com.ruoyi.order.domain.app.Vo.UserAddr;
import com.ruoyi.order.domain.app.from.cart.CartFrom;
import com.ruoyi.order.mapper.ProductMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.ShoppingCarMapper;
import com.ruoyi.order.domain.ShoppingCar;
import com.ruoyi.order.service.IShoppingCarService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-04
 */
@Service
public class ShoppingCarServiceImpl implements IShoppingCarService {
    @Resource
    private ShoppingCarMapper shoppingCarMapper;

    @Resource
    private ProductMapper productMapper;
    /**
     * 查询购物车
     *
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public ShoppingCar selectShoppingCarById(Integer id) {
        return shoppingCarMapper.selectShoppingCarById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param shoppingCar 购物车
     * @return 购物车
     */
    @Override
    public List<ShoppingCar> selectShoppingCarList(ShoppingCar shoppingCar) {
        return shoppingCarMapper.selectShoppingCarList(shoppingCar);
    }

    /**
     * 新增购物车
     *
     * @param shoppingCar 购物车
     * @return 结果
     */
    @Override
    public int insertShoppingCar(ShoppingCar shoppingCar) {
        shoppingCar.setCreateTime(DateUtils.getNowDate());
        return shoppingCarMapper.insertShoppingCar(shoppingCar);
    }

    /**
     * 修改购物车
     *
     * @param shoppingCar 购物车
     * @return 结果
     */
    @Override
    public int updateShoppingCar(ShoppingCar shoppingCar) {
        return shoppingCarMapper.updateShoppingCar(shoppingCar);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShoppingCarByIds(String ids) {
        return shoppingCarMapper.deleteShoppingCarByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteShoppingCarById(Integer id) {
        return shoppingCarMapper.deleteShoppingCarById(id);
    }

    @Override
    public ShopMoneyVo getTotalPay(Integer [] basketIds) {
        ShopMoneyVo shopMoneyVo = new ShopMoneyVo();
        BigDecimal decimal = BigDecimal.ZERO;
        BigDecimal reduceMoney = BigDecimal.ZERO;
        Map<String,Object> map = new HashMap<>();
        if(basketIds.length>0){
            for (Integer basketId : basketIds) {
                ShoppingCar shoppingCar = shoppingCarMapper.selectShoppingCarById(basketId);
                map.clear();
                map.put("productID",shoppingCar.getProductID());
                Product product = productMapper.selectOne(map);
                decimal.add(product.getPrice().multiply(new BigDecimal(shoppingCar.getNum())));
            }
        }
        shopMoneyVo.setTotalMoneyMoney(decimal);
        shopMoneyVo.setFinalMoney(decimal);
        shopMoneyVo.setSubtractMoneyMoney(reduceMoney);
        return shopMoneyVo;
    }

    @Override
    public List<CartItem> getShopCartProductInfo(Integer userID) {
        Map<String,Object> map = new HashMap<>();
        map.put("userID",userID);
        List<CartItem> result = shoppingCarMapper.getShopCartProductInfo(map);
        return result;
    }

    @Override
    public int setCartChangeItem(CartFrom cartFrom,Integer userID) {
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setCreateTime(DateUtils.getNowDate());
        shoppingCar.setCreateUser(userID);
        shoppingCar.setNum(cartFrom.getCount());
        shoppingCar.setProductID(cartFrom.getProdId());
        shoppingCar.setSearchValue(cartFrom.getDistributionCardNo());
        return shoppingCarMapper.insertShoppingCar(shoppingCar);
    }

    @Override
    public List<CartItem> getShopCartProductInfoByBasketIds(Integer[] basketIds) {
        Map<String,Object> map = new HashMap<>();
        List<CartItem> result = new ArrayList<>();
        for (int i = 0; i < basketIds.length; i++) {
            map.clear();
            map.put("id",basketIds[i]);
            CartItem cartItem = shoppingCarMapper.getShopCartProductInfoByBasketId(map);
            if(cartItem!=null){
                result.add(cartItem);
            }
        }
        return result;
    }

    @Override
    public int getCount() {
        return shoppingCarMapper.getCount();
    }

    @Override
    public UserAddr getUserAddr(Integer basketId) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",basketId);
        map.put("default",1);
        UserAddr userAddr= shoppingCarMapper.getUserAddr(map);
        return userAddr;
    }


    @Override
    public boolean saveBatch(Collection<ShoppingCar> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<ShoppingCar> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<ShoppingCar> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(ShoppingCar entity) {
        return false;
    }

    @Override
    public ShoppingCar getOne(Wrapper<ShoppingCar> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<ShoppingCar> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<ShoppingCar> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<ShoppingCar> getBaseMapper() {
        return null;
    }

    @Override
    public Class<ShoppingCar> getEntityClass() {
        return null;
    }
}
